package main

import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"image/color"
	"time"
	"transfersMonitor/view"
)

func main() {
	a := app.NewWithID("budao.monitor.transaction")
	a.Settings().SetTheme(&view.MyTheme{})
	w := a.NewWindow("transfers")
	w.SetMaster()

	title := [][]string{[]string{"item1", "item2", "item3", "item4", "item5"}}

	data := [][]string{[]string{"top left1", "top right", "top3", "top4", "top5"}}

	temp := mergeTwoDimensionalArray(title, data, false)
	fmt.Println(temp)
	list := widget.NewTable(
		func() (int, int) {
			return len(temp), len(title[0])
		},
		func() fyne.CanvasObject {
			return widget.NewLabel("wide content")
		},
		func(i widget.TableCellID, o fyne.CanvasObject) {
			o.(*widget.Label).SetText(temp[i.Row][i.Col])
		})
	data = append(data, []string{"b1", "b2", "b3", "b4", "b5"})
	//fmt.Println(data)
	temp = mergeTwoDimensionalArray(title, data, false)
	//fmt.Println(temp)
	data = mergeArray(data, []string{"c1", "c2a", "c3", "c4", "c5"}, true)
	//fmt.Println(data)
	temp = mergeTwoDimensionalArray(title, data, false)

	w.SetContent(list)

	binding.bind

	data = mergeArray(data, []string{"d1", "d2b", "d3", "d4", "d5"}, true)
	//fmt.Println(data)
	temp = mergeTwoDimensionalArray(title, data, false)

	//data = append(data, []string{"d1", "d2b", "d3", "d4"})

	//view, _ := view.Monitor(w)
	//w.SetContent(view)

	//w.SetContent(container.NewVBox(
	//	NewButtonColored(color.Black, color.White, func() {}),
	//	NewButtonColored(color.White, color.Black, func() {}),
	//	NewButtonColored(color.NRGBA{R: 0, G: 0, B: 180, A: 255}, color.NRGBA{R: 0, G: 180, B: 0, A: 255}, func() {}),
	//	NewButtonColored(color.NRGBA{R: 0, G: 180, B: 0, A: 255}, color.NRGBA{R: 0, G: 0, B: 180, A: 255}, func() {}),
	//))

	w.ShowAndRun()

	temp = add(title, data)
}

func timeadd(title [][]string, data [][]string) [][]string {
	var result [][]string
	for i := 0; i < 10; i++ {
		result = add(title, data)
		time.Sleep(time.Second * 3)
	}
	return result
}

func add(title [][]string, data [][]string) [][]string {
	temp := time.Now().Format("15:04:05")
	newData := []string{temp + "-1", temp + "-2", temp + "-3", temp + "-4", temp + "-5"}
	data = mergeArray(data, newData, true)
	return mergeTwoDimensionalArray(title, data, false)
}

func mergeTwoDimensionalArray(one [][]string, two [][]string, prefix bool) (result [][]string) {
	result = make([][]string, len(one)+len(two))

	if prefix == true {
		copy(result, two)
		copy(result[len(two):], one)
	} else {
		copy(result, one)
		copy(result[len(one):], two)
	}
	return
}

func mergeArray(one [][]string, two []string, prefix bool) (result [][]string) {
	result = make([][]string, len(one)+1)

	if prefix == true {
		result[0] = two
		copy(result[1:], one)
	} else {
		copy(result, one)
		result[len(one)] = two
	}
	return
}

func NewButtonColored(bc color.Color, tc color.Color, tapped func()) *fyne.Container {
	btn := widget.NewButton("", tapped)
	txtColored := canvas.NewText("a", tc)
	txtColored.Alignment = fyne.TextAlignCenter
	btnColored := canvas.NewRectangle(bc)
	contain := container.New(
		layout.NewMaxLayout(),
		btnColored,
		txtColored,
		btn,
	)
	return contain
}
